﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion


#region Usings

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.UI.View.Controls;
using ScrumTable.UI.View.Extension;
using ScrumTable.UI.View.Localization;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.ViewModel;

#endregion
namespace ScrumTable.UI.View.WinTouch.UserControls
{
   

    /// <summary>
    /// Interaction logic for SurfaceUserControl1.xaml
    /// </summary>
    public partial class UserStoryControl : ScrumItemControlBase, IDropUserControl
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private readonly ClickUtil _click = new ClickUtil();
        private readonly ClickUtil _clickTouch = new ClickUtil();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
 

        /// <summary>
        /// DependencyProperty for CanFlip.
        /// </summary>
        public static readonly DependencyProperty CanFlipProperty =
            DependencyProperty.Register("CanFlip",
                                        typeof(Boolean),
                                        typeof(UserStoryControl),
                                        new PropertyMetadata(true));
        /// <summary>
        /// Gets or sets if the User Story can be flipped.
        /// </summary>
        /// <value>True, if User Story can be flipped.</value>
        public Boolean CanFlip
        {
            get { return (Boolean)GetValue(CanFlipProperty); }
            set
            {
                SetValue(CanFlipProperty, value);
            }
        }



        #endregion

        #region Constructors / Destructor

        /// <summary>
        /// Initializes a new instance of the <see cref="UserStoryControl"/> class.
        /// </summary>
        
        public UserStoryControl()
        {

            DataContextChanged += OnDataContextChanged;
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                InitializeComponent();
            }
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        private bool ShowBackGround(Point position, bool isTouch)
        {
            if (!CanFlip || PresentationSource.FromVisual(this) == null || (isTouch ? !_clickTouch.IsClickInTimeAndPlace(position) : !_click.IsClickInTimeAndPlace(position)) || !(DataContext is UserStoryViewModel))
                return false;

            var story = ((UserStoryViewModel) DataContext).Original;
            if(story == null) {return false;}

            var item = new UserStoryControlBack
            {
                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Left,
                DataContext = DataContext
            };


            ShowItem(story, item, position, LanguageDictionary.Current.Translate<string>("UserStory_Titel_Back", "Content"));
            return true;
        }
       

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private void OnUserStoryPreviewContactDown(object sender, TouchEventArgs e)
        {
            e.Handled = ShowBackGround(e.GetTouchPoint(this).Position, true);
        }


        private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = ShowBackGround(e.GetPosition(this), false);
        }

        private void OnUserStoryDrop(object sender, SurfaceDragDropEventArgs e)
        {
            if (e.Cursor.Data is MemberViewModel)
                ((UserStoryViewModel)DataContext).AssignedMember = ((MemberViewModel)e.Cursor.Data);
            else if (e.Cursor.Data is TaskViewModel && !((TaskViewModel)e.Cursor.Data).Original.Parent.Equals(((UserStoryViewModel)DataContext).Original))
                ((UserStoryViewModel)DataContext).Tasks.Add(((TaskViewModel)e.Cursor.Data).Original);
            else if (e.Cursor.Data is ScrumPokerCard)
            {
                var card = ((ScrumPokerCard)e.Cursor.Data);
                if(card.CardType == ScrumPokerCardType.Number)
                {
                    ((UserStoryViewModel) DataContext).StoryPoints = card.Number;
                }
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
                
        }

        private void OnUserStoryPreviewQueryTarget(object sender, QueryTargetEventArgs e)
        {
            if (CanDrop(e.Cursor.Data) != CanDropResult.CanDrop)
            {
                e.UseDefault = false;
                e.ProposedTarget = null;
                e.Handled = true;
            }
        }


        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (DataContext is UserStory)
            {
                DataContext = new UserStoryViewModel((UserStory)DataContext);
            }
        }

        #endregion


        public FrameworkElement GetRootControl
        {
            get { return DropRoot; }
        }

        public bool ShowIconWithbackground
        {
            get { return true; }
        }

        public CanDropResult CanDrop(object objectToDrop)
        {
            return this.ConvertBoolToEnum(objectToDrop is MemberViewModel || objectToDrop is TaskViewModel || objectToDrop is ScrumPokerCard);
        }
    } 
  
}
